package com.itheima.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.dto.SysRoleDto;
import com.itheima.youdianda.entity.SysRole;
import com.itheima.youdianda.entity.SysUser;
import com.itheima.youdianda.service.SysRoleService;
import com.itheima.youdianda.vo.SysRoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sys_role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 角色列表-分页
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public R<Page> list(int page,int limit){
        //创建分页构造器
        Page<SysRole> pageInfo = new Page<>(page,limit);
        //创建条件构造器
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件
        queryWrapper.orderByAsc(SysRole::getCreate_date);
        //分页查询
        sysRoleService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }


    /**
     * 角色列表-全部
     * @return
     */
    @GetMapping("/allData")
    public R<List<SysRole>> allData(){
        List<SysRole> roles = sysRoleService.list();
        return R.success(roles);
    }


    /**
     * 角色添加
     * @param sysRoleDto
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody SysRoleDto sysRoleDto){
        log.info(sysRoleDto.toString());
        sysRoleService.saveWithMenu(sysRoleDto);
        return R.success("添加成功");
    }


    /**
     * 管理员详情,用于管理员修改的数据回显
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R<SysRoleVo> rowInfo(@RequestParam String id){
        log.info("管理员的id为{}",id);
        //添加条件构造器
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRole::getId,id);
        SysRole role = sysRoleService.getOne(queryWrapper);
        return R.success(null);
    }
}
